//获取应用实例
var app = getApp()
var util = require('../../utils/util.js')
var config = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    isShare: 0
  },

  toStart: function (e) {
    wx.navigateTo({
      url: "../loading/loading?fromType=1",
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.bgMusic.stop();
    app.globalData.bgMusicT.play();
    // this.setData({
    //   nickName: app.globalData.userInfo.nickName
    // });
    // console.log(nickName)
    this.getData();
  },



  getData: function () {
    var that = this;
    wx.request({
      url: config.ruleList,
      method: "POST",
      data: {
        type: 1
      },
      success: function (res) {
        console.log(config.ruleList)
        if (res.data.success == 0) {
          that.setData({
            rules: res.data.rows.categoryIntro,
            Img: res.data.rows.categoryImg
          });
        }
      }
    });

  },

  /**
    * 判断用户是否有机会游戏
    */
  IsCanPlay: function (e) {
    var that = this;
    wx: wx.request({
      url: config.isCanPlays,
      data: {
        openId: app.globalData.openid
      },
      method: 'POST',
      success: function (res) {
        console.log(config.isCanPlays)
        console.log(JSON.stringify(res.data))
        /**
        * 1=有  0=无
        */
        that.setData({
          times: res.data.rows.remainTimes,
        })
        console.log(res.data.rows)
        if (res.data.rows.times >= 1) {
          that.toStart();
        } else {
          wx.showModal({
            title: '提示',
            content: '您今天的闯关机会已经用完咯！',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
        return;
      },

    })

  },
  onShow: function () {

    app.globalData.bgMusicT.play();
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.globalData.bgMusic.play();
    app.globalData.bgMusicT.stop();
  },
  /**
  * 邀请好友
  */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    wx: wx.request({
      url: config.getOneChances,
      data: {
        openId: app.globalData.openid
      },
      method: 'POST',
      success: function (res) {
        
      },

    })
    return {
      title: app.globalData.userInfo.nickName + '邀请你答题冲榜！',
      path: '/pages/index/index?pageId=' + config.calendar,
    }
  },

  /**
     * 弹窗
     */
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.hideModal();
  }

  /**
   * 分享
   */
  // onShareAppMessage: function (res) {
  //   if (res.from === 'button') {
  //     // 来自页面内转发按钮
  //     console.log(res.target)
  //   }
  //   return {
  //     title: app.globalData.userInfo.nickName + '邀请你答题冲榜！',
  //     path: '/pages/index/index?pageId='+ config.calendar
  //   }
  // }
})